﻿using Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AGVControlSystemPro
{
    public partial class FrmDebug : Form
    {
        public FrmDebug()
        {
            InitializeComponent();
            this.dgvTrafficView.AutoGenerateColumns = false;
            this.timer1.Start();
        }
        private void ShowDebugInfo(string ip, byte[] buffer)
        {
            string txt = new SerialPortHelper.AlgorithmHelper().BytesTo16(buffer, SerialPortHelper.Enum16Hex.Blank);
            string info = "\r\n【" + ip + "】" + DateTime.Now.ToString() + "：" + txt + "\r\n";
            this.txtInfo.Invoke(new Action<string>(s => { this.txtInfo.AppendText(s); }), info);
        }      
        private void FrmDebug_FormClosed(object sender, FormClosedEventArgs e)
        {
            CommonMethods.isDisplyTraffic = false;//交管显示线程关闭
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (CommonMethods.isDisplyTraffic)
            {
                CommonMethods.isDisplyTraffic = false;
                this.dgvTrafficView.Invoke(new Action<List<TrafficInfo>>(s =>
                {
                    this.dgvTrafficView.AutoGenerateColumns = false; this.dgvTrafficView.DataSource = null; this.dgvTrafficView.DataSource = (s.Count != 0) ? s : null;
                }), CommonMethods.ListPreTraffic);
                //Thread.Sleep(1000);
            }
        }

        private void 清空ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CommonMethods.ListPreTraffic.Clear();//清空列表
            CommonMethods.isDisplyTraffic = true;
        }

        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.dgvTrafficView.CurrentRow == null || this.dgvTrafficView.Rows.Count == 0)
            {
                MessageBox.Show("交管信息删除失败！","交管删除");
                return;
            }
            CommonMethods.ListPreTraffic.RemoveAt(this.dgvTrafficView.CurrentRow.Index); //删除该项
            CommonMethods.isDisplyTraffic = true;
        }   
    
    }
}
